﻿using CCWin;
using PDMan.staticSource;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PDMan
{
    public partial class form1 : Skin_Mac
    {
        public form1()
        {
            var value = System.Configuration.ConfigurationSettings.AppSettings["dbsetNameSpace"];
            var value1 = System.Configuration.ConfigurationSettings.AppSettings["modelNamespace"];
            if (string.IsNullOrWhiteSpace(value1))
            {
                value1 = value;
            }
            InitializeComponent();
            skinComboBox1.SelectedIndex = 0;
            textBox_NameSpace.Text = value;
            Model_NameSpace.Text = value1;
        }

        private void SkinButton1_Click(object sender, EventArgs e)
        {
            CacheModel.NameSpace = textBox_NameSpace.Text;
            CacheModel.Model_NameSpace = Model_NameSpace.Text;

            try
            {
                CacheModel.ModelQz = Table_Text.Text; 
                if (!(Souce_Check.Checked && CacheModel.Init))
                {
                    OpenPDMan();
                }

                if (!(Target_Check.Checked && CacheModel.Init))
                {
                    OpenSavePath();
                }

                var result = Builder.ToCSharp(CacheModel.PDManPath, CacheModel.SavePath);
                if (result.Success)
                {
                    if (skinCheckBox1.Checked)
                    {
                        System.Diagnostics.Process.Start("explorer.exe", CacheModel.SavePath);
                    }
                    writeLog("生成代码成功");
                    CacheModel.Init = true;
                }
                else
                {
                    MessageBoxEx.Show(result.Details);
                }
            }
            catch (Exception ex)
            {
                writeLog(ex.Message);
                writeLog("程序异常,详情如下:");
            }
        }

        public void writeLog(string text)
        {
            Log.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  " + Log.Text.Insert(0, text + "\r\n");
        }

        private void OpenPDMan()
        {

            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Title = "选择PDMan文件";
                ofd.Filter = "PDMan文件|*.json";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    CacheModel.PDManPath = ofd.FileName;
                    writeLog("已选择PDMan路径");
                }
            }
        }

        private void OpenSavePath()
        {
            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                fbd.Description = "指定实体代码保存目录（将会覆盖已有文件）。";
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    CacheModel.SavePath = fbd.SelectedPath;
                    writeLog("已选择保存路径");
                }
            }
        }

        private void Souce_Check_CheckedChanged(object sender, EventArgs e)
        {
            if (Souce_Check.Checked)
                writeLog("已锁定源");
            else
                writeLog("已取消锁定源");
        }

        private void Target_Check_CheckedChanged(object sender, EventArgs e)
        {
            if (Target_Check.Checked)
                writeLog("已锁定目标");
            else
                writeLog("已取消锁定目标");
        }

        private void SkinCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (skinCheckBox1.Checked)
                writeLog("已锁定完成后打开");
            else
                writeLog("已取消锁定完成后打开"); 
        }
    }
}
